
$(document).ready(function() {
	$submit_panel = $('#submit-panel');
	$test = $('.test-results');
	
	success = $('#success').val();
	if(success) {
		beginTrace();
		trace(success);
		endTrace(false);
	} else {
		$submit_panel.show();	
		error = $('#error').val();
		if(error) {
			beginTrace();
			trace(error);
			endTrace(true);
		}
	}
	
	$('#edit').click(function() {
		$('form').submit();
	});

	$('#save').click(function(evt) {
		var title = $('#title').val();
		var message = "Adding library '" + title + "' to the BookBurrito database.\n\n"
				+ "Once saved, you will no longer be able to make any changes to the added library!";
				
		if(!confirm(message)) {
			return false;
		}
		
		$submit_panel.hide();
		$test.show();

		beginTrace();
		trace("Validating and saving '" + title + "'...\n");
	});
	
	function trace(msg) {
		$test.text($test.text() + msg);
	}
	
	function beginTrace() {
		$test.text('').removeClass('succeeded').removeClass('failed').show();
	}
	
	function endTrace(error) {
		$test.addClass(error ? 'failed' : 'succeeded');
		if(error) {
			$submit_panel.show();
		}
	}
		
});
